package com.demo.util;

import java.util.Date;
import java.util.Map;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JwtUtil {

    public final static String secret = "12321345455";

    public static String createToken(Map<String, Object> claims, Date exp) {
        JwtBuilder builder = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret);
        builder.setExpiration(exp);
        return builder.compact();
    }

    public static Date getExpireDate(Claims claims) {
        return claims.getExpiration();
    }

    public static Claims parseToken(String token) {
        try {
            return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            return null;
        }

    }

    public static String getUserName(Claims claims) {
        return getValue(claims, "username");
    }

    public static String getValue(Claims claims, String key) {
        return claims.getOrDefault(key, "").toString();
    }

}
